package org.example;


import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;

import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainApp {
    public static void main(String[] args) {
        // 加载 OpenCV 库
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        // 初始化模块
        ImageCapture imageCapture = new ImageCapture("path/to/camera/folder");
        ImageDisplay imageDisplay = new ImageDisplay();
        List<Mat> imageBuffer = new ArrayList<>();

        while (true) {
            // 获取最新图片
            List<File> imageFiles = imageCapture.getLatestImages(10);
            for (File file : imageFiles) {
                Mat image = imageCapture.loadImage(file);
                imageBuffer.add(image);
            }

            // 对齐和叠加图片
            Mat stackedImage = ImageStacker.stackImages(imageBuffer);

            // 显示图片
            BufferedImage bufferedImage = matToBufferedImage(stackedImage);
            imageDisplay.updateImage(bufferedImage);

            // 保存图片
            ImageSaver.saveImage(stackedImage, "path/to/output/stacked_image.png");

            // 清空缓冲区
            imageBuffer.clear();
        }
    }

    private static BufferedImage matToBufferedImage(Mat mat) {
        MatOfByte mob = new MatOfByte();
        Imgcodecs.imencode(".png", mat, mob);
        byte[] byteArray = mob.toArray();
        return (BufferedImage) new ImageIcon(byteArray).getImage();
    }

}